home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 4 / Apprentice-Release4.iso / Source Code / Add-Ons / BBEdit / MacBob 1.0ß2 / Source / Bob / Bob.h < prev    next >
Encoding:
C/C++ Source or Header  |  1995-12-12  |  9.7 KB  |  362 lines  |  [TEXT/KAHL]

  1. /***
  2.   *
  3.   *    Bob.h - bob definitions
  4.   *
  5.   *    Original code: Copyright (c) 1991, by David Michael Betz.  All rights reserved
  6.   *    Modifications and additions: Copyright © by Christopher E. Hyde, 1995
  7.   *
  8.   ***/
  9.  
  10. #include "MacBob.h"
  11.  
  12. // Limits
  13. enum {
  14.     kLineSize        =    256,
  15.     kLineLen        =    kLineSize - 1,
  16.     kLitStrLen        =    250,        // max literal string length
  17.     kLitStrSize,
  18.     kIdLen            =    50,            // max name/id length
  19.     kIdSize,
  20.     kMaxStack        =    500,        // runtime stack size
  21.     kTypeNameSize    =    16,
  22.     kCodeSize        =    31 * 1024,    // max code size
  23.     kMaxLoops        =    10,            // max level of embeded loops
  24.     kAllocUnits        =    10000        // number of allocation units per segment
  25. };
  26.  
  27. // stack manipulation macros
  28. #define    check(n)            { if (sp - (n) < stkbase) StackOver(); }
  29. #define    push(x,t,f)        (--sp, sp->fType = (t), sp->f = (x))
  30. #define    push_integer(x)    push(x,tInteger,fInt)
  31. #define    push_string(x)        push(x,tString,fStr)
  32. #define    push_class(x)        push(x,tClass,fClass)
  33. #define    push_object(x)        push(x,tObject,fObject)
  34. #define    push_bytecode(x)    push(x,tByteCode,fVec)
  35. #define    push_var(x)        push(x,tVar,fVar)
  36. #define    push_nil()            (--sp, sp->fType = tNil)
  37. #define    IsType(o,t)        (sp[o].fType == t)
  38. #define    IsNotType(o,t)        (sp[o].fType != t)
  39.  
  40. // macros to set values
  41. #define    set(s,x,t,f)        ((s)->f = (x), (s)->fType = (t))
  42. #define    set_integer(s,x)    set(s,x,tInteger,fInt)
  43. #define    set_class(s,x)        set(s,x,tClass,fClass)
  44. #define    set_object(s,x)        set(s,x,tObject,fObject)
  45. #define    set_code(s,x)        set(s,x,tCode,fCode)
  46. #define    set_bytecode(s,x)    set(s,x,tByteCode,fVec)
  47. #define    set_dictionary(s,x)    set(s,x,tDict,fDict)
  48. #define    set_var(s,x)        set(s,x,tVar,fVar)
  49. #define    set_string(s,x)        set(s,x,tString,fStr)
  50. #define    set_vector(s,x)    set(s,x,tVector,fVec)
  51. #define    set_iostream(s,x)    set(s,x,tStream,fStream)
  52. #define    set_nil(s)            ((s)->fType = tNil)
  53.  
  54. // value field access macros
  55. #define    valtype(x)            ((x)->fType)
  56. #define    isnil(x)                ((x)->fType == tNil)
  57.  
  58. // class field access macros
  59. #define    claddr(x)                ((x)->fClass)
  60. #define    clgetname(x)            (&claddr(x)->cl_name)
  61. #define    clgetbase(x)            (&claddr(x)->cl_base)
  62. #define    clgetmembers(x)        (&claddr(x)->cl_members)
  63. #define    clgetfunctions(x)        (&claddr(x)->cl_functions)
  64. #define    clgetsize(x)            (claddr(x)->cl_size)
  65.  
  66. // object field access macros
  67. #define    objaddr(x)            ((x)->fObject)
  68. #define    objgetclass(x)            (&objaddr(x)->fClass)
  69. //#define    objgetmember(x,i)        (&objaddr(x)->fMembers[i])
  70. //#define    objsetmember(x,i,v)        (objaddr(x)->fMembers[i] = (v))
  71.  
  72. // Vector field access macros
  73. #define    vecaddr(x)            ((x)->fVec)
  74. //#define    vecgetelement(x,i)        (&vecaddr(x)->fData[i])
  75. //#define    vecsetelement(x,i,v)    (vecaddr(x)->fData[i] = (v))
  76. #define    VLen(x)                ((x)->fVec->fLength)
  77. #define    VData(x)                ((x)->fVec->fData)
  78. #define    VItem(x, i)            ((x)->fVec->fData[i])
  79.  
  80. // String field access macros
  81. #define    SLen(x)                ((x)->fStr->fLength)
  82. #define    SData(x)                ((x)->fStr->fData)
  83.  
  84. // Dictionary field access macros
  85. #define    digetclass(x)            (&(x)->fDict->fClass)
  86. #define    DictContents(x)            ((x)->fDict->fContents)
  87.  
  88. // dictionary entry field access macros
  89. #define    deaddr(x)                ((x)->fVar)
  90. #define    degetdictionary(x)        (&deaddr(x)->fDict)
  91. #define    degetkey(x)            (&deaddr(x)->fKey)
  92. #define    degetvalue(x)            (&deaddr(x)->fValue)
  93. #define    degetnext(x)            (&deaddr(x)->fNext)
  94. #define    degettype(x)            (deaddr(x)->fType)
  95.  
  96. // i/o stream access macros
  97. #define    ios_t(x)                ((x)->fStream->fStream)
  98. #define    iosclose(x)            (ios_t(x)->Close())
  99. #define    iosgetc(x)                (((CIStream*) ios_t(x))->Get())
  100. #define    iosputc(c,x)            (((COStream*) ios_t(x))->Put((c)))
  101. #define    iosputs(s,x)            (((COStream*) ios_t(x))->Put((s)))
  102.  
  103. #define    _DefOpFn(n)            void n (int argc)
  104.  
  105. struct TValue;
  106. struct THead;
  107. struct TClass;
  108. struct TObject;
  109. struct TVector;
  110. struct TString;
  111. struct TDict;
  112. struct TEntry;
  113. struct TStream;
  114.  
  115. typedef _DefOpFn((*OpFn));            // Built-in function prototype
  116.  
  117. // Basic/low level types
  118. typedef int TToken;
  119. typedef char* Str;
  120. typedef const char* KStr;
  121. typedef char TId[kIdSize];
  122.  
  123. typedef THead*        Head;
  124. typedef TObject*    Object;
  125. typedef TClass*    Class;
  126. typedef TVector*    Vector;
  127. typedef TString*    String;
  128. typedef TDict*        Dict;
  129. typedef TEntry*    Entry;
  130. typedef TStream*    Stream;
  131. typedef TValue*    Value;
  132. typedef const TValue* ConstValue;
  133.  
  134. // Value descriptor structure
  135. struct TValue {
  136.   int            fType;                // data type
  137.   union {                            // value:
  138.     SInt32        fInt;                //    integer
  139.     Class        fClass;                //    class (in heap)
  140.     Object        fObject;            //    object (in heap)
  141.     Vector        fVec;                //    vector (in heap)
  142.     String        fStr;                //    string (in heap)
  143.     Dict        fDict;                //    dictionary (in heap)
  144.     Entry        fVar;                //    variable (in heap)
  145.     OpFn        fCode;                //    code for built-in function
  146.     Stream        fStream;            //    i/o stream (in heap)
  147.     Head        fHead;                //    (used by garbage collector)
  148.     Value        fChain;                //    (used by garbage collector)
  149.   };
  150. };
  151.  
  152. // Useful definitions
  153. enum {
  154.     kValueSize        =    sizeof(TValue),
  155.     kMark            =    1,
  156.         // Element indices in bytecode vector
  157.     kIByteCodes = 0, kIClass, kIName, kIFirstLit
  158. };
  159.  
  160. struct THead {
  161.     char    fHType;
  162.     char    fFlags;
  163.     Value    fChain;
  164. };
  165.  
  166. struct TClass : THead {
  167.     short    cl_size;
  168. #if 1
  169.     TValue    cl_name;
  170.     TValue    cl_base;
  171.     TValue    cl_members;
  172.     TValue    cl_functions;
  173. #else
  174.     String    cl_name;
  175.     Class    cl_base;
  176.     Dict    cl_members;
  177.     Dict    cl_functions;
  178. #endif
  179. //    int        cl_size;
  180. };
  181.  
  182. struct TObject : THead {
  183.     TValue    fClass;
  184.     TValue    fMembers[1];
  185. static inline int CalcSize (int n)
  186.         { return sizeof(TObject) - kValueSize + n * kValueSize; }
  187. };
  188.  
  189. struct _TVec : THead {
  190.     int        fLength;
  191. };
  192.  
  193. struct TVector : _TVec {
  194.     TValue    fData[1];
  195. static inline int CalcSize (int n)
  196.         { return sizeof(_TVec) + n * kValueSize; }
  197. };
  198.  
  199. struct TString : _TVec {
  200.     char    fData[1];
  201. static inline int CalcSize (int n)
  202.         { return sizeof(_TVec) + n; }
  203. };
  204.  
  205. struct TDict : THead {
  206.     TValue    fClass;
  207.     TValue    fContents;
  208. };
  209.  
  210. // Dictionary entry structure
  211. struct TEntry : THead {
  212.     short    fType;        // symbol type
  213.     TValue    fDict;        // backpointer to dictionary
  214.     TValue    fKey;        // symbol name
  215.     TValue    fValue;        // symbol value
  216.     TValue    fNext;        // next entry
  217. };
  218.  
  219. // I/O stream structure
  220. struct TStream : THead {
  221.     CStream*    fStream;
  222. };
  223.  
  224. // Symbol types
  225. enum {
  226.     _st0 = 0,
  227.     stClass,        // class definition
  228.     stData,            // data member
  229.     stSData,        // static data member
  230.     stFunction,        // function member
  231.     stSFunction        // static function member
  232. };
  233.  
  234. // Data types
  235. enum {
  236.     _tMin = 0, _tFirstMinus1 = _tMin - 1,
  237.  
  238.     tNil, tInteger, tCode,
  239.     tClass, tObject, tVector, tString,
  240.     tByteCode, tDict, tVar, tStream,
  241.  
  242.     _tLastPlus1, _tMax = _tLastPlus1 - 1,
  243.     _tMarkMin = tClass, _tMarkMax = tStream
  244. };
  245.  
  246. // Opcodes
  247. enum {
  248.     _opNoOp,
  249.     opBRT,        // 01    branch on true
  250.     opBRF,        // 02    branch on false
  251.     opBR,        // 03    branch unconditionally
  252.     opNIL,        // 04    load top of stack with nil
  253.     opPUSH,        // 05    push nil onto stack
  254.     opNOT,        // 06    logical negate top of stack
  255.     opNEG,        // 07    negate top of stack
  256.     opADD,        // 08    add top two stack entries
  257.     opSUB,        // 09    subtract top two stack entries
  258.     opMUL,        // 0A    multiply top two stack entries
  259.     opDIV,        // 0B    divide top two stack entries
  260.     opREM,        // 0C    remainder top two stack entries
  261.     opBAND,        // 0D    bitwise and top two stack entries
  262.     opBOR,        // 0E    bitwise or top two stack entries
  263.     opXOR,        // 0F    bitwise xor top two stack entries
  264.     opSHL,        // 10    shift left top+1 stack by TOS
  265.     opSHR,        // 11    shift right top+1 stack by TOS
  266.     opBNOT,        // 12    bitwise not top of stack
  267.     opLT,        // 13    less than
  268.     opLE,        // 14    less than or equal to
  269.     opEQ,        // 15    equal to
  270.     opNE,        // 16    not equal to
  271.     opGE,        // 17    greater than or equal to
  272.     opGT,        // 18    greater than
  273.     opINC,        // 19    increment
  274.     opDEC,        // 1A    decrement
  275.     opLIT,        // 1B    load literal
  276.     opRTS,        // 1C    return from subroutine
  277.     opCALL,        // 1D    call a function
  278.     opREF,        // 1E    load a variable value
  279.     opSET,        // 1F    set the value of a variable
  280.     opVREF,        // 20    load a vector element
  281.     opVSET,        // 21    set a vector element
  282.     opMREF,        // 22    load a member variable value
  283.     opMSET,        // 23    set a member variable
  284.     opAREF,        // 24    load an argument value
  285.     opASET,        // 25    set an argument value
  286.     opTREF,        // 26    load a temporary variable value
  287.     opTSET,        // 27    set a temporary variable
  288.     opTSPC,        // 28    allocate temporary variable space
  289.     opSEND,        // 29    send a message to an object
  290.     opDUP2,        // 2A    duplicate top two elements on the stack
  291.     opNEW,        // 2B    create a new class object
  292.     opINT,        // 2C    push a short int
  293.     _opLastPlus1, _opLast = _opLastPlus1 - 1,
  294.     _opFirst = _opNoOp + 1
  295. };
  296.  
  297. // External variables
  298. extern Value stkbase, sp, fp, stktop;
  299. extern TValue gNil;
  300. extern TValue stdin_iostream, stdout_iostream, stderr_iostream;
  301.  
  302. // External routines
  303.  
  304. // Compiler.cp
  305. void    InitCompiler        (void);
  306. void    MarkCompiler        (void);
  307. void    CompileDefinitions    (CIStream& iStream);
  308.  
  309. // Bob.cp
  310. void    BobMain            (void);
  311. void    PrintErrF        (KStr fmt, ...);
  312. void    PrintF            (KStr fmt, ...);
  313. void    Info            (KStr fmt, ...);
  314. void    Error            (KStr fmt, ...);
  315. void    Put                (KStr str);
  316.  
  317. // Scanner.cp
  318. void    InitScanner        (CIStream& iStream);
  319. TToken    Token            (void);
  320. KStr    TokenName        (TToken token);
  321. void    ParseError        (KStr msg);
  322.  
  323. // Interpreter.cp
  324. void    Execute            (KStr name);
  325. void    Arg0Not            (int type);
  326. void    Arg0NotInt        (void);
  327. void    Arg1NotInt        (void);
  328. void    BadType            (int off, int type);
  329. void    StackOver        (void);
  330.  
  331. // Debug.cp
  332. void    DumpProcedure    (ConstValue code);
  333. //int        DumpInstruction    (ConstValue code, int lc);
  334. int        DumpInstruction    (const TVector* code, int lc);
  335.  
  336. // Functions.cp
  337. void    Print            (Value ios, bool quoteIt, ConstValue val);
  338. void    InitFunctions    (void);
  339.  
  340. // Memory.cp
  341. void    Initialize        (int maxStack);
  342. Entry    AddEntry        (Value dict, KStr key, int type);
  343. Entry    FindEntry        (ConstValue dict, KStr key);
  344. String    MakeString        (KStr str, int length);
  345. String    MakeString        (KStr str);
  346. char*    GetCString        (char* buf, int max, Value str);
  347. String    NewString        (int n);
  348. Object    NewObject        (Value aClass);
  349. Vector    NewVector        (int n);
  350. Class    NewClass        (KStr name, Value base);
  351. Dict    NewDict            (Value aClass);
  352. Stream    NewIOStream        (CStream& stream);
  353. void    GC                (void);
  354. void    Mark            (Value val);
  355.  
  356. #pragma noreturn(Error)
  357. #pragma noreturn(Arg0Not)
  358. #pragma noreturn(Arg0NotInt)
  359. #pragma noreturn(Arg1NotInt)
  360. #pragma noreturn(BadType)
  361. #pragma noreturn(StackOver)
  362.